const { success,fail } = require("../toast");
const jwt = require("jsonwebtoken");
const key = "web";

module.exports = function (router) {
    router.post("/login",async ctx=> {
    const {name,pwd}= ctx.request.body;
   if(name==="admin" && pwd==="123456") {
    const token = jwt.sign({name: "admin"},key, {expiresIn:300 });
   return success(ctx, "Bearer " + token);
   }
   else{
       return fail(ctx,"账号或密码不正确!");
   }
    }).get("/data",async ctx=> {
        
    try{
      const user= jwt.verify(token, key);
      if(user.name=== "root") {
       return success(ctx, {});
      }else{
          return fail(ctx,"权限不足");
      }
    } catch (error) {
  return fail(ctx, error);
    }
    })
}